<script setup>
import {
  getUser,
  addUser,
  updateUser,
  checkUserNameUnique
} from "@/api/system/user";
import { listBizCompany } from "@/api/biz/bizCompany";
import { listDeptTree } from "@/api/system/dept";

const props = defineProps({
  id: {
    type: [Number, String],
    default: ""
  },
  user: {
    type: String,
    default: ""
  }
});

const formRef = ref();
const data = ref({
  loading: false,
  form: {
    id: props.id,
    userType: "1",
    status: "0"
  },
  rules: {
    name: [{ required: true, message: "请输入姓名", trigger: "blur" }],
    // companyId: [{ required: true, message: "请选择公司", trigger: "blur" }],
    userName: [
      { required: true, message: "请输入登陆账号", trigger: "blur" },
      { validator: getcheckUserNameUnique }
    ]
  },
  deptTreeList: [],
  postsList: [],
  rolesList: [],
  listCompany: []
});

onMounted(() => {
  if (data.value.form.id != "") {
    getInfo();
  }
  listDeptTree("", 3).then((res) => {
    data.value.deptTreeList = res.data;
  });
  getUser().then((response) => {
    data.value.postsList = response.posts;
    data.value.rolesList = response.roles;
  });
  getListCompany();
});
// 获取公司
function getListCompany() {
  listBizCompany({ pageSize: 50 }).then((res) => {
    data.value.listCompany = res.data.list;
  });
}
// 得到节点
function getnode(node, prop, event) {
  console.log("node", node, prop, event);
  data.value.form.parentId = node.id;
}
function getInfo() {
  data.value.loading = true;
  getUser(props.id).then((res) => {
    data.value.loading = false;
    data.value.form = res.data;
    data.value.form.postIds = res.postIds;
    data.value.form.roleIds = res.roleIds;
  });
}

function getcheckUserNameUnique(rule, value, callback) {
  if (props.id != "" || props.id != undefined) {
    callback();
  }
  const msg = "登陆账号已存在";
  if (value === "") {
    callback();
  } else {
    const checkData = {
      userName: value
    };
    checkUserNameUnique(checkData).then((response) => {
      if (response.data.code === "1") {
        callback();
      } else {
        callback(msg);
      }
    });
  }
}
defineExpose({
  submit(callback) {
    if (data.value.form.id == "") {
      formRef.value.validate((valid) => {
        if (valid) {
          addUser(data.value.form).then(() => {
            ElMessage.success({
              message: "保存成功",
              center: true
            });
            callback && callback();
          });
        }
      });
    } else {
      formRef.value.validate((valid) => {
        if (valid) {
          updateUser(data.value.form).then(() => {
            ElMessage.success({
              message: "修改成功",
              center: true
            });
            callback && callback();
          });
        }
      });
    }
  }
});
</script>

<template>
  <div v-loading="data.loading">
    <el-form
      ref="formRef"
      :model="data.form"
      :rules="data.rules"
      label-width="120px"
      label-suffix=" "
      label-position="top"
      style="padding: 0 20px"
    >
      <!-- <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="所属公司" prop="companyId">
            <el-select v-model="data.form.companyId" style="width: 100%">
              <el-option
                :value="item.id"
                :label="item.companyName"
                v-for="(item, index) in data.listCompany"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row> -->
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="姓名" prop="name">
            <el-input v-model="data.form.name" placeholder="请输入姓名">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="登录名称" prop="userName">
            <el-input v-model="data.form.userName" placeholder="请输入登录名称">
            </el-input>
          </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
          <el-form-item label="用户编号" prop="no">
            <el-input v-model="data.form.no" placeholder="请输入用户编号">
            </el-input>
          </el-form-item>
        </el-col> -->
        <el-col :span="12">
          <el-form-item label="部门" prop="deptId">
            <el-tree-select
              placeholder="请选择部门"
              v-model="data.form.deptId"
              :data="data.deptTreeList"
              :default-expand-all="true"
              node-key="id"
              @node-click="getnode"
              :check-strictly="true"
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
        <!--
        <el-col :span="12">
          <el-form-item label="岗位" prop="postIds">
            <el-select
              v-model="data.form.postIds"
              placeholder="请选择岗位"
              multiple
              style="width: 100%"
            >
              <el-option
                v-for="item in data.postsList"
                :key="item.id"
                :label="item.postName"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col> -->
        <el-col :span="12">
          <el-form-item label="角色" prop="roleIds">
            <el-select
              v-model="data.form.roleIds"
              placeholder="请选择角色"
              multiple
              style="width: 100%"
            >
              <el-option
                v-for="item in data.rolesList"
                :key="item.id"
                :label="item.roleName"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="电话" prop="phonenumber">
            <el-input v-model="data.form.phonenumber" placeholder="请输入电话">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态" prop="status">
            <el-radio-group v-model="data.form.status">
              <el-radio-button label="0">在职</el-radio-button>
              <el-radio-button label="1">离职</el-radio-button>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
          <el-form-item label="用户类型" prop="userType">
            <el-radio-group v-model="data.form.userType">
              <el-radio-button label="0">平台</el-radio-button>
              <el-radio-button label="1">商户</el-radio-button>
            </el-radio-group>
          </el-form-item>
        </el-col> -->
      </el-row>
    </el-form>
  </div>
</template>
